﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using MiniErp.Entity.DataStreams;
using MiniErp.Entity.DataStreams.DOs;
using MiniErp.Entity.DataStreams.DOs.AssetManagement;
using MiniErp.Entity.DataStreams.DOs.Notice;
using MiniErp.Entity.DataStreams.DOs.WeeklyReport;
using System.Collections.Generic;

namespace MiniErp.Common.DB.MySqlDB
{
    /// <summary>
    /// mysql数据库上下文
    /// </summary>
    public class MysqlContext : DbContext
    {
        /// <summary>
        /// 依赖注入数据库上下文对象
        /// </summary>
        public MysqlContext(DbContextOptions<MysqlContext> options) : base(options) { }
        /// <summary>
        /// 配置实体模型
        /// </summary>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {

            modelBuilder.Entity<UserInfo>()
                .Navigation(d => d.AssetInfos)
                .AutoInclude(true);
            modelBuilder.Entity<Rights>();
            modelBuilder.Entity<Department>();
            modelBuilder.Entity<Position>();
            modelBuilder.Entity<Titles>();
            modelBuilder.Entity<Systems>();
            #region Rights
            modelBuilder.Entity<Jobs>();
            modelBuilder.Entity<JobsSystemsRights>();
            #endregion
            #region AssetManagement
            modelBuilder.Entity<AssetState>();
            modelBuilder.Entity<GeneralType>();
            modelBuilder.Entity<SecondaryType>();
            modelBuilder.Entity<AssetInfo>().HasKey(item => new { item.AId, item.GeneralTypeId, item.SecondaryTypeId });
            modelBuilder.Entity<AssetModificationRecord>();
            modelBuilder.Entity<AssetModificationType>();
            #endregion
            #region  Notice
            modelBuilder.Entity<JobsNotices>();
            modelBuilder.Entity<UserInfoNoticesReply>();
            modelBuilder.Entity<Notices>();
            #endregion
            #region WeeklyReport
            modelBuilder.Entity<WeekInfo>();
            modelBuilder.Entity<WeeklyInfo>();
            modelBuilder.Entity<WorkItems>();
            #endregion
        }
        /// <summary>
        /// 配置事件
        /// </summary>
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            //启用懒加载==》Microsoft.EntityFrameworkCore.Proxi
            optionsBuilder.UseLazyLoadingProxies();
#if DEBUG
            var loggerFactory = new LoggerFactory();
            loggerFactory.AddProvider(new EFCoreLoggerProvider());
            //添加EnableSensitiveDataLogging，启用敏感数据记录
            optionsBuilder.UseLoggerFactory(loggerFactory).EnableSensitiveDataLogging();
#endif
        }

    }
}
